package com.system.service.impl;

import com.common.enums.BaseResponseCode;
import com.common.utils.AjaxResult;
import com.system.entity.SysRoleEntity;
import com.system.mapper.SysRoleMapper;
import com.system.service.ISysRoleMenuService;
import com.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 数据添加
     *
     * @param sysRoleEntity
     */

    @Override
    @Transactional
    public AjaxResult sysRoleAdd(SysRoleEntity sysRoleEntity) {

        List<Long> list1 = new ArrayList<>();

        for (String id : sysRoleEntity.getList()) {
            String[] list = id.split(",");
            for (String s : list) {
                list1.add(Long.valueOf(s));
            }
        }
        sysRoleEntity.setCreateTime(new Date());
        sysRoleEntity.setUpdateTime(new Date());
            /*
              添加角色
             */
        sysRoleMapper.sysRoleAdd(sysRoleEntity);
            /*
              获取角色id
             */
        long userId = sysRoleMapper.getRoleId(sysRoleEntity.getRoleName());
            /*
                添加角色拥有的菜单
             */
        sysRoleMenuService.sysRoleMenuAddList(userId, list1);
        return new AjaxResult(BaseResponseCode.ADD_SUCCESS.getCode(), BaseResponseCode.ADD_SUCCESS.getMsg());
    }

    /**
     * 删除与批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult sysRoleDel(List<Integer> ids) {
        sysRoleMapper.sysRoleDel(ids);
        return new AjaxResult(BaseResponseCode.DELETE_SUCCESS.getCode(), BaseResponseCode.DELETE_SUCCESS.getMsg());
    }

    /**
     * 更新
     *
     * @param sysRoleEntity
     */
    @Override
    @Transactional
    public AjaxResult sysRoleUpById(SysRoleEntity sysRoleEntity) {

        sysRoleEntity.setUpdateTime(new Date());
        /*
        更新角色表
         */
        sysRoleMapper.sysRoleUpById(sysRoleEntity);

        /*
        删除原来的菜单
         */
        List<Long> list = new ArrayList<>();
        list.add(sysRoleEntity.getRoleId());
        sysRoleMenuService.sysRoleMenuDel(list);

        /**
         *  添加角色拥有的菜单
         */
        List<Long> list1 = new ArrayList<>();

        for (String id : sysRoleEntity.getList()) {
            String[] str = id.split(",");
            for (String s : str) {
                list1.add(Long.valueOf(s));
            }
        }
        sysRoleMenuService.sysRoleMenuAddList(sysRoleEntity.getRoleId(), list1);
        return new AjaxResult(BaseResponseCode.UPDATE_SUCCESS.getCode(), BaseResponseCode.UPDATE_SUCCESS.getMsg());

    }

    /**
     * 查询单条数据
     */
    @Override
    public SysRoleEntity sysRoleGetById(Long id) {
        return sysRoleMapper.sysRoleGetById(id);
    }


    /**
     * 查询数据库
     *
     * @return
     */
    @Override
    public AjaxResult sysRoleSelectListByPage(SysRoleEntity sysRoleEntity) {
        Integer count = sysRoleMapper.sysRoleSelCount();
        List<SysRoleEntity> list = sysRoleMapper.sysRoleSelectListByPage(sysRoleEntity);
        return AjaxResult.data(count, list);
    }

    /**
     * 修改角色管理角色的启用与停用
     *
     * @param status
     * @return
     */
    @Override
    public AjaxResult upStatus(Integer status) {
//        sysRoleMapper.upStatus(status);
        return null;
    }
}